/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean;

import applicationexcepcion.MarchioRegistratoException;
import ejb.common.MarchioManagerLocal;
import ejb.registrazione.RegistrazioneLocal;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.context.FacesContext;
import org.apache.commons.io.IOUtils;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.UploadedFile;
import utility.ExceptionThrower;
import utility.ResourceBundleLoader;

/**
 *
 * @author Carmela
 */
public class RegistrazioneMarchio {
    @EJB
    private MarchioManagerLocal marchioManager;
    @EJB
    private RegistrazioneLocal registrazione;
    
    private List<entity.Marchio> marchi;
    
    public StreamedContent getFotoStream(entity.Marchio m){
        System.out.println(m.getFoto().getFoto().length);
        DefaultStreamedContent dsc=new DefaultStreamedContent(new ByteArrayInputStream(m.getFoto().getFoto()));
        return dsc;
    }

    public List<entity.Marchio> getMarchi() {
        return marchi;
    }

    public void setMarchi(List<entity.Marchio> marchi) {
        this.marchi = marchi;
    }
    
    public void registraMarchio(FileUploadEvent event){
        ResourceBundle rb=ResourceBundleLoader.getResourceBundle(FacesContext.getCurrentInstance());
        InputStream is=null;
        String nomeFile="";
        try {
            UploadedFile uf=event.getFile();
            is = uf.getInputstream();
            nomeFile=uf.getFileName();
            StringTokenizer st=new StringTokenizer(nomeFile, ".");
            String nomeMarchio="";
            if(st.hasMoreTokens()){
                nomeMarchio=st.nextToken();
            }
            byte[] foto=IOUtils.toByteArray(is);
            registrazione.registrazioneMarchio(nomeMarchio, foto, nomeFile);
            init();
            ExceptionThrower.throwMessageInfo(FacesContext.getCurrentInstance(), rb, "marchioRegistrazioneInfoM", nomeFile);
        } catch (IOException ex) {
            Logger.getLogger(RegistrazioneMarchio.class.getName()).log(Level.SEVERE, null, ex);
        } catch(MarchioRegistratoException ex){
            ExceptionThrower.throwMessageError(FacesContext.getCurrentInstance(), rb, "marchioRegistrazioneErrorM", nomeFile); 
        }
        finally {
            try {
                is.close();
            } catch (IOException ex) {
                Logger.getLogger(RegistrazioneMarchio.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    /**
     * Creates a new instance of Marchio
     */
    public RegistrazioneMarchio() {
    }
    
    @PostConstruct
    public void init(){
        this.marchi=marchioManager.cercaTutto();
    }
}
